
/*
 * PredictionModuleBridgeFromTextFile.cpp
 *
 *  Created on: Dec 4, 2014
 *      Authors: Avihay Avitan & Stefan Farcas
 */

#include "PredictionModuleBridgeFromTextFile.h"

namespace PredictionModuleBridgeSpace {

/**
 * empty constructor
 */
PredictionModuleBridgeFromTextFile::PredictionModuleBridgeFromTextFile() : PredictionModuleBridge() {

	 this->parser=NULL;
}

PredictionModuleBridgeFromTextFile::~PredictionModuleBridgeFromTextFile() {
        // TODO Auto-generated destructor stub
	if(parser!=NULL) delete(parser);
}
/**
 * sets the text file parser
 */
void PredictionModuleBridgeFromTextFile::setTextFileParser(ITextFileParser * parser)
{
        this->parser=( PredictionTextFileParser * ) parser;
}

//@override
bool PredictionModuleBridgeFromTextFile::loadPredictionData( PassPrioritizer * pprio)
{
		if (pprio == NULL)
			return false;

        vector<PassVector*> myVec;
		
		if (CoreSettings::getInstance().isPredictionFromTextFile())
		{
		    vector<PassVector*> myVec;
			if (parser->parseFile())
			{
					myVec = parser->getParsedPassVectors();

					for (int i = 0 ; i < 3 ; i++)
					{
							pprio->add(myVec.back());
							myVec.pop_back();
					}
			}
		}
		else
		{
			createPredictionData(pprio);
		}
		
        return true;
}

void PredictionModuleBridgeFromTextFile::createPredictionData( PassPrioritizer * pprio)
{
	long startPassTime = getNowTimeWithSecOffset(CoreSettings::getInstance().getPredictionOffsetFromNow());
	long interval = CoreSettings::getInstance().getPredictionAccessInterval();
	int durationUpperLimit =CoreSettings::getInstance().getDurationUpperLimit();
	int durationLowerLimit = CoreSettings::getInstance().getDurationLowerLimit();
	int durationToRand = durationUpperLimit-durationLowerLimit +1;

	srand (startPassTime);

	for(int i=0; i<3 ; i++)
	{
		PassVector * pass = new PassVector();
		pass->setSatId("Sat"+int2str(i+1));
		Access access;
		access.setStartTime(startPassTime);
		long duration = rand() % durationToRand + durationLowerLimit;
		access.setDuration(duration);
		access.setEndTime(startPassTime+duration);
		access.setInterval(interval);
		pass->setAccess(access);
		int numOfaers = duration/interval;
		
		for (int j=0;j<numOfaers;j++)
		{
			SatelliteAER aer;
			aer.setTimeStamp(startPassTime+(interval*j));
			srand (aer.getTimeStamp());
			aer.setAzimuth(rand() % 360);
			srand (aer.getTimeStamp());
			aer.setElevation(rand() % 10000);
			srand (aer.getTimeStamp());
			aer.setRange(rand() % 100);
			pass->addInstance(aer);
		}
		
        pprio->add(pass);
		srand (startPassTime);
		startPassTime += CoreSettings::getInstance().getStartPassInterval();
    }
}

} /* namespace PredictionModuleBridge */
